require(['./config'],()=>{
    require(['template','request','header','footer'],(template,request)=>{
       class Cate{
        constructor(){
            this.init()
        }
        init(){
            const id=window.location.hash.slice(1,2)
            // console.log(id)
            var start=0;
            var sort=0;
            var n=1;
            //根据id区请求当前分类的数据
            request.getCategory(id,start,sort).then(resp=>{
                console.log(resp)
                if(resp.code===200){
                    const{list}=resp.data.items
                $('#goodsClassify').html(template('classifyTemplate',{list}))
                }
            })
            //价格最低
            $('.shop-sort').on('click','.lowest-price',function(){
                // console.log(123)
                sort=1;
                request.getCategory(id,start,sort).then(resp=>{
                    if(resp.code===200){
                        const{list}=resp.data.items
                    $('#goodsClassify').html(template('classifyTemplate',{list}))
                    }
                })

            })
            //销量
            $('.shop-sort').on('click','.sales-num',function(){
                // console.log(123)
                sort=2;
                request.getCategory(id,start,sort).then(resp=>{
                    if(resp.code===200){
                        const{list}=resp.data.items
                    $('#goodsClassify').html(template('classifyTemplate',{list}))
                    }
                })
            })
            //综合
            $('.shop-sort').on('click','.composite',function(){
                // console.log(123)
                sort=0;
                request.getCategory(id,start,sort).then(resp=>{
                    if(resp.code===200){
                        const{list}=resp.data.items
                    $('#goodsClassify').html(template('classifyTemplate',{list}))
                    }
                })
            })

            //向后分页
            $('.paging').on('click','.next-page',function(){
                start +=20;
                n++;
                request.getCategory(id,start,sort).then(resp=>{
                    if(resp.code===200){
                        const{list}=resp.data.items
                    $('#goodsClassify').html(template('classifyTemplate',{list}))
                    }
                })
                $('#page-num').html(n)
            })

            //向前分页
            $('.paging').on('click','.previous-page',function(){
                if(start==0){
                    start=0;
                    n=1
                }else{
                    start -=20;
                    n--;
                }
                request.getCategory(id,start,sort).then(resp=>{
                    if(resp.code===200){
                        const{list}=resp.data.items
                    $('#goodsClassify').html(template('classifyTemplate',{list}))
                    }
                })
                $('#page-num').html(n)
            })

        }
       }
       new Cate()
    })
})